package com.baozun.service.impl;


import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baozun.mapper.TContactMapper;
import com.baozun.model.TContact;
import com.baozun.model.TContactExample;
import com.baozun.service.ContactService;
import com.baozun.util.PageData;

@Service("contactService")
@Transactional
public class ContactServiceImpl implements ContactService {
    @Autowired
    private TContactMapper contactMapper;

    @Override
    public void add(TContact bean) {
        if (StringUtils.isEmpty(bean.getStContactId())) {
            bean.setStContactId(UUID.randomUUID().toString());
        }
        bean.setDtCreateTime(new Date());
        contactMapper.insert(bean);
    }

    @Override
    public TContact get(String id) {
        return contactMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(TContact bean) {
        bean.setDtUpdateTime(new Date());
        contactMapper.updateByPrimaryKeySelective(bean);
    }

    @Override
    public void delete(String stContactId) {
        contactMapper.deleteByPrimaryKey(stContactId);
    }

    @Override
    public PageData<TContact> getPageListByExample(TContactExample record, int pageSize, int currentPage) {
        // TODO Auto-generated method stub
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TContact> infoList = new PageData<TContact>();
        List<TContact> appendixList = contactMapper.selectByExample(record);
        int count = contactMapper.countByExample(record);
        infoList.addAll(appendixList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }

    @Override
    public List<TContact> getList(TContactExample record) {
        // TODO Auto-generated method stub
        return contactMapper.selectByExample(record);
    }
}
